/*
	flad : Copyright 2010 Dale J Williams

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/
package lads.flad.scene {
	
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;
	import lads.flad.flad;
	import lads.flad.GameCore;
	import lads.flad.GameEngine;
	import lads.flad.interfaces.IScene;
	import lads.flad.load.LoadManager;
	import lads.flad.scene.cameras.Camera;
	
	/**
	 * GameScene: A base class for the hosts of all in-game content.
	 * @author Dale J Williams
	 * @version 1.1
	 */
	public class GameScene extends SceneObject implements IScene {
		
		private var layers		:Vector.<SceneLayer>;
		private var objects		:Vector.<SceneObject>;		
		private var _engine		:GameEngine;
		private var _camera		:Camera;
		private var _screen		:Rectangle;
		private var _core		:GameCore;
		private var _loader		:LoadManager;
		private var _xml		:XML;
		
		public function GameScene(name:String) {
			super(name);
			init();
		}
			
		private function init():void {
			layers = new Vector.<SceneLayer>();
			objects = new Vector.<SceneObject>();
			_screen = new Rectangle();
			addLayer("default");
			enableMouse(false);
		}
		
		public function startAll():void {
			trace("Scene " + name + " started.");
			if (!camera) _camera = new Camera("defaultCamera");
			
			flad::_start();
			for each(var object:SceneObject in objects)
				object.flad::_start();
				
			camera.start();
			
			screen.x = 0;
			screen.y = 0;
			screen.width = core.width;
			screen.height = core.height;
		}
		
		public function stopAll():void {
			trace("Scene " + name + " stopped.");
			flad::_stop();
			for each(var object:SceneObject in objects)
				object.flad::_stop();
				
			camera.stop();
		}
		
		public function updateAll():void {
			flad::_update();
			for each(var object:SceneObject in objects)
				object.flad::_update();
				
			camera.update();	
			updateScreen();
		}
		
		protected function updateScreen():void {
			scaleX = scaleY = camera.zoom;
			screen.x = camera.x*camera.zoom - screen.width * 0.5;
			screen.y = camera.y*camera.zoom - screen.height* 0.5;
			x = -screen.x;
			y = -screen.y;		
		}
		
		public function enableMouse(value:Boolean):void {
			mouseEnabled = mouseChildren = value;
		}
		
		public function addLayer(layerOrLayerName:Object, index:int = -1):SceneLayer {
			var layer:SceneLayer;
			if (getLayerFromObject(layerOrLayerName))
				throw new Error("Layer already in scene");
			if (layerOrLayerName is String)
				layer = new SceneLayer(layerOrLayerName as String);
			else layer = layerOrLayerName as SceneLayer;
			
			if (index < 0) index = numChildren;
			
			layers.push(layer);			
			layer.scene = this;
			addChildAt(layer, index);
			
			return layer;
		}
		
		public function removeLayer(layerOrLayerName:Object):SceneLayer {
			var layer:SceneLayer = getLayerFromObject(layerOrLayerName);
			var i:int = layers.length;
			while (i--) {
				if (layers[i] != layer) continue;
				removeChild(layer);
				layers.splice(i, 1);
				layer.scene = null;
				return layer;
			}
			return null;
		}
		
		private function getLayerFromObject(layerOrLayerName:Object):SceneLayer {
			if (layerOrLayerName is SceneLayer)
				return layerOrLayerName as SceneLayer;
			else if (layerOrLayerName is String) {
				return getLayerByName(layerOrLayerName as String);
			} else return null;
		}
		
		public function getLayerByName(name:String):SceneLayer {
			var i:int = layers.length;
			while (i--)
				if (layers[i].name == name)
					return layers[i];
			return null;
		}
				
		public function addToScene(object:SceneObject, layerOrLayerName:Object="default"):SceneObject {
			if (hasSceneObject(object)) removeFromScene(object);
			objects.push(object);
			var layer:SceneLayer = getLayerFromObject(layerOrLayerName);
			layer.addChild(object);
			object.scene = this;
			object.flad::_start();
			return object;
		}
		
		public function getSceneObjectAt(index:int):SceneObject {
			return objects[index];
		}
		
		public function getSceneObject(name:String):SceneObject {
			var i:int = objects.length;
			while (i--)
				if (objects[i].name == name)
					return objects[i];
			return null;
		}
		
		public function hasSceneObject(object:SceneObject):Boolean {
			var i:int = objects.length;
			while (i--)
				if (objects[i] == object)
					return true;
			return false;
		}
		
		public function removeFromScene(object:SceneObject):SceneObject {
			var i:int = objects.length;
			while (i--) {
				if (objects[i] != object) continue;
				object.flad::_stop();
				object.parent.removeChild(object);
				objects.splice(i, 1);
				object.scene = null;
				return object;
			}
			return null;
		}
		
		public function removeAllFromScene():void {
			var i:int = objects.length;
			while (i--) removeFromScene(objects[i]);
		}
		
		public function setCamera(camera:Camera):void {
			_camera = camera;
		}
		
		public function isWithin(x:Number, y:Number):Boolean {
			return x > 0 && x < width && y > 0 && y < height;
		}
		
		public function get engine():GameEngine { return _engine; }		
		public function set engine(value:GameEngine):void {
			_engine = value;
			_core = value ? _engine.core : null;
		}
		
		public function get numObjects():int { return objects.length }
		
		public function get camera():Camera { return _camera; }		
		
		override public function get core():GameCore { return _core; }
		
		/**
		 * The XML That loaded the scene's assets.
		 */
		public function get xml():XML { return _xml; }		
		public function set xml(value:XML):void {
			_xml = value;
		}
		
		/**
		 * The loader that loaded the scene's assets.
		 */
		public function get loader():LoadManager { return _loader; }		
		public function set loader(value:LoadManager):void {
			_loader = value;
		}
		
		public function get screen():Rectangle { return _screen; }
		
		public function get host():DisplayObject {
			return this;
		}
		
	}

}